Macromedia Flex Article

Learning Flex Basics (Part 3) : Working with Containers


Table of Contents

  1. Introduction
  2. Building the Containers
  3. Create the Application Object
  4. Create a Data Array
  5. Add a TabNavigator Container for Global Navigation
  6. Add an HBox and Panel Containers for Main Views
  7. Add a ViewStack, LinkBar, and Panel Containers for Secondary Views
  8. Add a Form Container
  9. Add a Tile Layout Container and Populate It Using a Repeater
  10. Create a Pseudo Bar Chart Using a Repeater and VBoxes
  11. Complete Code Listing for containers.mxml
  12. Where to Go from Here

Add a Tile Layout Container and Populate It Using a Repeater

For the second view in the first main view, you use a Tile layout container, which lays out child items in rows or columns depending how the direction property is set (the default is rows). The Tile container fits items as well as it can with the space available, wrapping rows or columns as necessary.

You can enter the child elements of the Tile container manually, but the more typical use case for the container is where it will contain an a set of items that are generated by iterating over an array of data whose length may change over time, or is not known until runtime, because the data is obtained from an external source.

You iterate over a data array using the Repeater class, which takes an array as its dataProvider. During the iteration, the currentIndex of the array and the currentItem of data are available to you programmatically. In this exercise you generate VBox containers that contain a Label; the VBox container is similar to the HBox container you used earlier, but arranges its children vertically rather than horizontally.

For runtime events, in addition to the properties of the generated items, you can access the original data item used to generate it through the getRepeaterItem() method of the target property of the event object; the target is the object on which the event occurred. In this exercise, you handle the event mouseDown over repeated items.

  1. Inside the Panel with the title "Tile," add a Tile container with a width of 400:

    <mx:Tile width="400">
    </mx:Tile>
    
  2. Inside the Tile block, add a Repeater with id of myRep and the dataProvider bound to myArray:

    <mx:Repeater id="myRep" dataProvider="{myArray}">
    </mx:Repeater>
    
  3. Inside the Repeater block, add a VBox container with borderStyle of solid and the mouseDown event set to call a function called setStatus(), passing event.target.getRepeaterItem():

    <mx:VBox borderStyle="solid" mouseDown="setStatus(event.target.getRepeaterItem())">
    </mx:VBox>
    
  4. Inside the VBox container, add a Label with the text property set to index: {myRep.currentIndex} - item: {myRep.currentItem}:

    <mx:Label text="index: {myRep.currentIndex} - item: {myRep.currentItem}"/>
    
  5. Inside the script block at the top of the application, add function block for the setStatus() function; the function takes a parameter called item of type Number, and returns nothing:

    function setStatus(item:Number):Void
    {
    }
    
  6. Inside the function block, add a single line that sets the status property of the surrounding Panel to "Item: "+item:

    tilePanel.status="Item: "+item;
    

    Note: Remember that the item passed to the function is the original data item associated with the repeated object where the event occurred.

  7. Save the file and browse it. Click the repeated VBox containers to see the associated data displayed in the Panel container status area.